//
//  UserPutViewController.m
//  HGMMT
//
//  Created by eport_user1 on 2018/4/17.
//  Copyright © 2018年 eport_user1. All rights reserved.
//

#import "UserPutViewController.h"
#import "UserModel.h"
#import "DataSave.h"
#import "LinkServers.h"
#import "MBProgressHUD+NJ.h"
#import "SelectUserViewController.h"
#import "LYYNavigationController.h"




@interface UserPutViewController ()
@property (weak, nonatomic) IBOutlet UITextField *guidTextfield;
@property (weak, nonatomic) IBOutlet UIImageView *imgView1;
@property (weak, nonatomic) IBOutlet UITextField *gyTextField;
@property (weak, nonatomic) IBOutlet UIImageView *imgView2;
@property (weak, nonatomic) IBOutlet UITextField *usernameTextfield;
@property (weak, nonatomic) IBOutlet UIImageView *imgView3;
@property (weak, nonatomic) IBOutlet UITextField *dyncTextfield;
@property (weak, nonatomic) IBOutlet UIImageView *imgView4;
@property (weak, nonatomic) IBOutlet UIButton *finishedButton;

@end

@implementation UserPutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self _initUI];
    
   // [self receiveMessage];
    [self initRightButton];
    
    
    //接受通知
    [self receiveNot];
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark -处理界面
- (IBAction)finishedButtonAction:(UIButton *)sender {
    
    if (self.guidTextfield.text.length > 0 && self.gyTextField.text.length > 0 && self.usernameTextfield.text.length > 0 && self.dyncTextfield.text.length > 0)
    {
        
        
        UserModel *model = [[UserModel alloc]init];
        model.guid = self.guidTextfield.text ? self.guidTextfield.text : @"";
        model.usercoder = self.gyTextField.text ? self.gyTextField.text : @"";
        model.username = self.usernameTextfield.text ? self.usernameTextfield.text : @"";
        model.dept = self.dyncTextfield.text ? self.dyncTextfield.text : @"";
        [[DataSave shareInstance] saveUserInfo:model];
        
        
        UIAlertController *alerVC = [UIAlertController alertControllerWithTitle:nil message:@"信息录入成功" preferredStyle:(UIAlertControllerStyleAlert)];
        [alerVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            self.guidTextfield.text = @"";
            self.gyTextField.text = @"";
            self.usernameTextfield.text = @"";
            self.dyncTextfield.text = @"";
            
        }]];
        
        [self presentViewController:alerVC animated:YES completion:nil];
        
    }
    else
    {
     
        UIAlertController *alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"信息录入的不完整" preferredStyle:(UIAlertControllerStyleAlert)];
        [alerVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            

            
        }]];
        
        [self presentViewController:alerVC animated:YES completion:nil];
        
        
        
        
    }
    
}


-(void)_initUI{
    
    self.imgView1.backgroundColor = [Tool colorWithHex:0xD5D5D5 alpha:1];
    self.imgView2.backgroundColor = [Tool colorWithHex:0xD5D5D5 alpha:1];
    self.imgView3.backgroundColor = [Tool colorWithHex:0xD5D5D5 alpha:1];
    self.imgView4.backgroundColor = [Tool colorWithHex:0xD5D5D5 alpha:1];
    
    self.guidTextfield.layer.borderColor = [UIColor blackColor].CGColor;
    self.guidTextfield.layer.borderWidth = 1;
    self.guidTextfield.layer.cornerRadius = 5;
    self.guidTextfield.layer.masksToBounds = YES;
    
    self.gyTextField.layer.borderColor = [UIColor blackColor].CGColor;
    self.gyTextField.layer.borderWidth = 1;
    self.gyTextField.layer.cornerRadius = 5;
    self.gyTextField.layer.masksToBounds = YES;
    
    self.usernameTextfield.layer.borderColor = [UIColor blackColor].CGColor;
    self.usernameTextfield.layer.borderWidth = 1;
    self.usernameTextfield.layer.cornerRadius = 5;
    self.usernameTextfield.layer.masksToBounds = YES;
    
    self.dyncTextfield.layer.borderColor = [UIColor blackColor].CGColor;
    self.dyncTextfield.layer.borderWidth = 1;
    self.dyncTextfield.layer.cornerRadius = 5;
    self.dyncTextfield.layer.masksToBounds = YES;
    
    
    self.finishedButton.backgroundColor = HGBTN_BackgoundColor;
    [self.finishedButton setTitleColor:HGBTN_TitleColor forState:(UIControlStateNormal)];
    //self.registerButton.layer.borderColor = [UIColor blackColor].CGColor;
    //self.registerButton.layer.borderWidth = 1;
    self.finishedButton.titleLabel.font = [UIFont systemFontOfSize:17];
    self.finishedButton.layer.cornerRadius = 5;
    self.finishedButton.layer.masksToBounds = YES;
    
    
}



-(void)initRightButton{
   
    
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"从IDE获取" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightButtionAction:)];
        self.navigationItem.rightBarButtonItem = rightItem;

}

-(void)rightButtionAction:(UIButton *)btn{
    HGLog(@"点击了导航栏右侧按钮");
    
   NSString *getUserString = @"0005:-L\n";
    //NSString *getUserString = @"0007:-L\n";
    NSData *userInfoData = [getUserString dataUsingEncoding:NSUTF8StringEncoding];
    [MBProgressHUD showMessage:@"正在获取用户信息" toView:self.view];
    
    [[LinkServers shareInstance].clientSocket writeData:userInfoData withTimeout:-1 tag:0];
    
}


#pragma mark -通知相关
-(void)receiveNot{
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notActionUserInfo:) name:GetUserInfo_not object:nil];
    
    
}

-(void)notActionUserInfo:(NSNotification *)not{
    
    /*
     @"state":
     @"userinfo*/
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
   //
   // BOOL state = [not.userInfo objectForKey:@"state"];
    
    NSNumber *num = [not.userInfo objectForKey:@"state"];
    BOOL state = [num boolValue];
    
    if (state) {
        
        NSArray *userInfo = [not.userInfo objectForKey:@"userinfo"];
        NSMutableArray *mutaArray = [NSMutableArray array];
        for (NSDictionary *dicx in userInfo) {
            
            /*
             @property(nonatomic,copy)NSString *guid;   //guid
             @property(nonatomic,copy)NSString *usercoder;  //官员号
             @property(nonatomic,copy)NSString *username;   //用户名称
             @property(nonatomic,copy)NSString *dept; //单位名称
             */
            
            /*
             {"id":1,"guid":"123456789","usercode":"2853135","username":"邢伟新","dept":"数据中心"}
             */
            
            UserModel *model = [[UserModel alloc]init];
            model.guid = [dicx objectForKey:@"guid"];
            model.usercoder = [dicx objectForKey:@"usercode"];
            model.username = [dicx objectForKey:@"username"];
            model.dept = [dicx objectForKey:@"dept"];
            
            [mutaArray addObject:model];
        }
        
        SelectUserViewController *selectVC = [[SelectUserViewController alloc]init];
        selectVC.userInfoArray = mutaArray;
        LYYNavigationController *lyy = [[LYYNavigationController alloc]initWithRootViewController:selectVC];
        [self presentViewController:lyy animated:YES completion:nil];
        
    }else{
        
        
        UIAlertController *alerVC = [UIAlertController alertControllerWithTitle:@"获取失败" message:@"请确认开发套件上是否录入了用户信息" preferredStyle:(UIAlertControllerStyleAlert)];
        [alerVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [self presentViewController:alerVC animated:YES completion:nil];
        
    }

}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.guidTextfield resignFirstResponder];
    [self.gyTextField resignFirstResponder];
    [self.usernameTextfield resignFirstResponder];
    [self.dyncTextfield resignFirstResponder];

}


@end
